package com.geofitti.andoird;

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends Activity{
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.geofitti_register);
		
		final TextView loginScreen = (TextView) findViewById(R.id.link_to_login);	
		loginScreen.setOnClickListener(new View.OnClickListener(){
			public void onClick(View arg0){
				loginScreen.setText("blah");
				finish();
			}
		});
		
		final Button registerButton = (Button) findViewById(R.id.btnRegister);	
		registerButton.setOnClickListener(new View.OnClickListener(){
			public void onClick(View arg0){
				String strConnect = connect("http://www.geofittiapp.com/index.php");
				registerButton.setText(strConnect); 
				
				//finish();
			}
		});
	}
	
	
	private static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
	
    public static String connect(String url)
    {
    	ThreadPolicy tp = ThreadPolicy.LAX;
    	StrictMode.setThreadPolicy(tp);
    	
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpget = new HttpPost(url); 
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);
                instream.close();
                return result;
            }
        } catch (ClientProtocolException e) {
        	return e.toString();
        } catch (IOException e) {
        	return e.toString();
        }
        return null;
    }

	
	
//	public JSONObject connect(String url)
//	{
//	 
//	HttpClient httpclient = new DefaultHttpClient();
//	 
//	// Prepare a request object
//	HttpGet httpget = new HttpGet(url);
//	 
//	// Execute the request
//	HttpResponse response;
//	 
//	JSONObject json = new JSONObject();
//	 
//	try {
//	response = httpclient.execute(httpget);
//	 
////	HttpEntity entity = response.getEntity();
//	 
////	if (entity != null) {
////	 
////	// A Simple JSON Response Read
////	InputStream instream = entity.getContent();
////	String result= convertStreamToString(instream);
////	 
////	json=new JSONObject(result);
////	 
////	instream.close();
////	}
//	 
//	} 
//	catch (ClientProtocolException e) {
////	 TODO Auto-generated catch block
//	e.printStackTrace();
//	} catch (IOException e) {
//	// TODO Auto-generated catch block
//	e.printStackTrace();
//	} 
//		
////	catch (JSONException e) {
////	// TODO Auto-generated catch block
////	e.printStackTrace();
////	}
//	 
//	return json;
//	}
//	/**
//	*
//	* @param is
//	* @return String
//	*/
//	public String convertStreamToString(InputStream is) {
//	BufferedReader reader = new BufferedReader(new InputStreamReader(is));
//	StringBuilder sb = new StringBuilder();
//	 
//	String line = null;
//	try {
//	while ((line = reader.readLine()) != null) {
//	sb.append(line + "\n");
//	}
//	} catch (IOException e) {
//	e.printStackTrace();
//	} finally {
//	try {
//	is.close();
//	} catch (IOException e) {
//	e.printStackTrace();
//	}
//	}
//	return sb.toString();
//	}
}
